﻿


function PlanProdDetalleProductoCtrl(obj) {

    var _I = this;
    this._obj = obj;

    _I.clientId = obj.id;

    this.Initialize = function () {

        // Cargo los eventos en los inputs
        $('#' + _I.clientId + ' .cPlanProdInputCant').change(function () {
            _I._changeEventForInput(this);
        });

        $('#' + _I.clientId + ' .cPlanProdInputCant').focus(function () {
            _I.focusEventForInput(this);
        });

        $('#' + _I.clientId + ' .cPlanProdInputCant').focusout(function () {
            _I.focusoutEventForInput(this);
        });

        $('#' + _I.clientId + ' .cPlanProdInputCant').keyup(function (e) {
            _I.arrowMovementEventForInput(this, e);
        });       
    };

    this._changeEventForInput = function (inputCtrl) {
        // sumo el total       
        var inputNameParsed = inputCtrl.id.split('_');
        var columna = inputNameParsed[2];        
        var fila = inputNameParsed[3];
        var inputTotalName = '#' + _I.clientId + '_planprodtotal_' + fila;
        var totalCtrl = $(inputTotalName);

        var columna = 0;
        var existeCtrl = true;
        var inputGenericName = '#' + _I.clientId + '_planprodcant_';
        var inputName = '';
        var total = 0;
        while (existeCtrl) {
            inputName = inputGenericName + columna + '_' + fila;
            var inputCtrl = $(inputName);
            if (inputCtrl.length != 0) {
                if (!isNaN(inputCtrl.val())) {
                    total += parseInt(inputCtrl.val());
                }
                else {
                    inputCtrl.val('0');
                }
            }
            else {
                existeCtrl = false;
            }

            columna++;
        }

        totalCtrl.val(total);

        _I._OnChangeTotal();
    }

    this.focusEventForInput = function (inputCtrl) {
        if ($(inputCtrl).attr('readonly') == '') {
            $(inputCtrl).addClass('cPlanProdInputCantWithFocus');
        }
        inputCtrl.select();
    }

    this.focusoutEventForInput = function (inputCtrl) {
        if ($(inputCtrl).attr('readonly') == '') {
            $(inputCtrl).removeClass('cPlanProdInputCantWithFocus');
        }
    }

    this.arrowMovementEventForInput = function (inputCtrl, e) {

        var key = e.which || e.keyCode;

        inputCtrl.value = inputCtrl.value.replace(/[^0-9\.]/g, '');
        if (inputCtrl.value == '') {
            inputCtrl.value = '0';
        }

        if (key == 37 || key == 38 || key == 39 || key == 40) {
            var sigoBuscando = true;
            while (sigoBuscando) {
                var inputNameParsed = inputCtrl.id.split('_');
                var columna = parseInt(inputNameParsed[2]);
                var fila = parseInt(inputNameParsed[3]);                               
                var inputGenericName = '#' + _I.clientId + '_planprodcant_';
                // abajo         
                if (key == 40) fila++;
                // derecha         
                if (key == 39) columna++;
                // arriba         
                if (key == 38) fila--;
                // izquierda         
                if (key == 37) columna--;

                var ctrlTargetName = inputGenericName + columna + "_" + fila;
                var inputCtrl = $(ctrlTargetName);
                if (inputCtrl.length != 0) {
                    if (inputCtrl.attr('removed') == 'true') {
                        sigoBuscando = true;
                        inputCtrl = inputCtrl.get(0);
                    }
                    else {
                        sigoBuscando = false;
                        inputCtrl.focus();
                        inputCtrl.select();
                    }
                }
                else {
                    sigoBuscando = false;
                }
            }
        }
    }

    this.RegisterChangeTotal = function (objFunction) {
        _I.changeTotalHandler = objFunction;
    }

    this._OnChangeTotal = function () {

        if (_I.changeTotalHandler) {
            var total = 0;
            $('input[id^=' + _I.clientId + '_planprodtotal_').each(function () {
                total += parseInt($(this).val());
            });

            _I.changeTotalHandler(total);
        }
    }       
}